package com.bleeth.fx.component;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONObject;
import com.bleeth.fx.helper.SStyleHelper;
import com.bleeth.fx.layout.BaseBuilder;
import com.bleeth.fx.layout.GirdBuilder;
import com.bleeth.fx.layout.RowBuilder;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
public class MTable {


    private List<String> idList = new ArrayList<>();

    private List<String> nameList = new ArrayList<>();

    private List<JSONObject> dataList = new ArrayList<>();

    private GirdBuilder girdBuilder;

    public static MTable create() {
        MTable mTable = new MTable();
        mTable.girdBuilder = GirdBuilder.create();
        return mTable;
    }


    public MTable column(String id,String name) {
        idList.add(id);
        nameList.add(name);
        return this;
    }

    /**
     * 添加多行
     * @param rowList
     */
    public void addRows(List<JSONObject> rowList) {

    }

    /**
     * 添加一行
     * @param row
     * @return
     */
    public void addRow(JSONObject row) {
    }


    public GirdBuilder make() {
        //header
        List<BaseBuilder> baseBuilders = new ArrayList<>();
        girdBuilder.addVBuilder(BaseBuilder.create().setHeight(50D));
        for (int i = 0; i < idList.size(); i++) {
            BaseBuilder baseBuilder = BaseBuilder.create().setWidth(200D).setPos(Pos.CENTER).setStyle(node -> setCellStyle(node));
            baseBuilders.add(baseBuilder);
        }
        girdBuilder.addRow(0, ArrayUtil.toArray(baseBuilders, BaseBuilder.class));
        for (int i = 0; i < idList.size(); i++) {
            Label label = new Label(idList.get(i));
            label.setAlignment(Pos.CENTER);
            girdBuilder.addCell(i, label);
        }
        //
        RowBuilder rowBuilder = girdBuilder.getRowBuilder(0);
        HBox header = rowBuilder.getRow();
        SStyleHelper.node(header)
                .addStyle("-fx-background-color", "#a5c1da")
                .addStyle("-fx-border-width", 1)
                .addStyle("-fx-background-radius", 5)
                .apply();

        return girdBuilder;
    }

    private void setCellStyle(Node node) {
        SStyleHelper.node(node)
                .addStyle("-fx-border-width", 3)
                .addStyle("-fx-border-color", "#a7bfd8")
                .addStyle("-fx-background-radius", 5)
                .apply();
    }


}
